package control;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import view.AbstractContentJPanel;
import view.NotCloseableException;

@SuppressWarnings("serial")
public class CloseCurrentTabAction extends AbstractAction
		implements ChangeListener {
	JTabbedPane pane;
	AbstractContentJPanel panel;

	public CloseCurrentTabAction(JTabbedPane tabbedPane) {
		super("Tab Schliessen");
		pane = tabbedPane;
		pane.addChangeListener(this);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

		stateChanged(null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			panel.tryToClose();
		} catch (NotCloseableException e1) {}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if(pane.getSelectedComponent() instanceof AbstractContentJPanel){
			panel = (AbstractContentJPanel) pane.getSelectedComponent();
			setEnabled(panel.isCloseable());
		}
	}

}
